package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntTldjkzdsyjlBaDetailBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntTldjkzdsyjlBaDetailVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntTldjkzdsyjlBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntTldjkzdsyjlBaDetailMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntTldjkzdsyjlBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土坍落度及扩展度经时损失试验检测记录详情Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntTldjkzdsyjlBaDetailServiceImpl implements IReportSnhntTldjkzdsyjlBaDetailService {

    private final ReportSnhntTldjkzdsyjlBaDetailMapper baseMapper;

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录详情
     */
    @Override
    public ReportSnhntTldjkzdsyjlBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSnhntTldjkzdsyjlBaDetailVo> queryPageList(ReportSnhntTldjkzdsyjlBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntTldjkzdsyjlBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土坍落度及扩展度经时损失试验检测记录详情列表
     */
    @Override
    public List<ReportSnhntTldjkzdsyjlBaDetailVo> queryList(ReportSnhntTldjkzdsyjlBaDetailBo bo) {
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBaDetail> buildQueryWrapper(ReportSnhntTldjkzdsyjlBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntTldjkzdsyjlBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSnhntTldjkzdsyjlBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), ReportSnhntTldjkzdsyjlBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getChujstld1() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujstld1, bo.getChujstld1());
        lqw.eq(bo.getChujstld2() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujstld2, bo.getChujstld2());
        lqw.eq(bo.getChujstldpj() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujstldpj, bo.getChujstldpj());
        lqw.eq(bo.getJingzhtld1() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhtld1, bo.getJingzhtld1());
        lqw.eq(bo.getJingzhtld2() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhtld2, bo.getJingzhtld2());
        lqw.eq(bo.getJingzhtldpj() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhtldpj, bo.getJingzhtldpj());
        lqw.eq(bo.getTanldjsss() != null, ReportSnhntTldjkzdsyjlBaDetail::getTanldjsss, bo.getTanldjsss());
        lqw.eq(bo.getChujskzd1() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujskzd1, bo.getChujskzd1());
        lqw.eq(bo.getChujskzd2() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujskzd2, bo.getChujskzd2());
        lqw.eq(bo.getChujskzdpj() != null, ReportSnhntTldjkzdsyjlBaDetail::getChujskzdpj, bo.getChujskzdpj());
        lqw.eq(bo.getJingzhkzd1() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhkzd1, bo.getJingzhkzd1());
        lqw.eq(bo.getJingzhkzd2() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhkzd2, bo.getJingzhkzd2());
        lqw.eq(bo.getJingzhkzdpj() != null, ReportSnhntTldjkzdsyjlBaDetail::getJingzhkzdpj, bo.getJingzhkzdpj());
        lqw.eq(bo.getKuozdjsss() != null, ReportSnhntTldjkzdsyjlBaDetail::getKuozdjsss, bo.getKuozdjsss());
        return lqw;
    }

    /**
     * 新增混凝土坍落度及扩展度经时损失试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSnhntTldjkzdsyjlBaDetailBo bo) {
        ReportSnhntTldjkzdsyjlBaDetail add = BeanUtil.toBean(bo, ReportSnhntTldjkzdsyjlBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改混凝土坍落度及扩展度经时损失试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSnhntTldjkzdsyjlBaDetailBo bo) {
        ReportSnhntTldjkzdsyjlBaDetail update = BeanUtil.toBean(bo, ReportSnhntTldjkzdsyjlBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntTldjkzdsyjlBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土坍落度及扩展度经时损失试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
